let app = getApp();

Page({
  data: {
    category: [], //列表
    detail: [], //列表对应的商品
    curIndex: '-2', //起始是hot类
    isScroll: true, //可以向下滑动
    toView: 'hot', //初始展示的物品类型
    num: 1, //当前页
    ifnum: true, //是否还有新的页数
    showPop: false,
    animationData: {},
    Length: 0,
    userID: null
  },

  onReady() {
    // console.log(this.data.detail);
    var self = this;
  },

  //下滑刷新函数
  loadMore() {
    let self = this;
    console.log('下滑刷新');
    if (self.data.ifnum) {
      if (self.data.toView != 'hot') {
        self.setData({
          num: self.data.num + 1
        })
        self.request();
        // self.incise();
      }
    }
  },

  //上滑游览
  refesh() {
    console.log('上滑游览');
  },

  onLoad() {
    let self = this;
    self.setData({
      userID: getApp().globalData.userID,
    })
    console.log('开始加载默认');
    console.log(getApp().globalData.userID);
    wx.request({
      url: 'https://localhost:80/goodsType/goodsTypes',
      method: 'GET',
      success: res => {
        if (res.statusCode == 200) {
          console.log(res);
          self.setData({
            category: res.data,
          });
          // self.calprice();
          // console.log(res.data);
        } else {
          console.log('状态码是：' + res.statusCode);
        }
      },
      error: res => {
        console.log('请求失败！');
      }
    })
    self.setData({
      toView: getApp().globalData.toView,
      curIndex: getApp().globalData.curIndex
    })
    self.request();
    self.incise();
    console.log('参数如下：');
    console.log(getApp().globalData.curIndex);
    console.log(getApp().globalData.curIndex);
  },
  //设置url函数
  urlChange(id, num) {
    return 'https://localhost:80/goods/' + id + '/' + num + '/goods';
  },
  //点击左边list触发
  switchTab(e) {
    const self = this;
    // console.log(e);
    // console.log(e.currentTarget.dataset.index);
    // console.log(e.target.dataset.id);
    self.setData({
      toView: e.target.dataset.id,
      curIndex: e.target.dataset.index,
      detail: [],
      num: 1,
      ifnum: true
    })
    self.request();
  },
  //请求goods函数
  request() {
    let self = this;
    wx.request({
      url: self.urlChange(self.data.toView, self.data.num),
      methods: 'GET',
      success: res => {
        // console.log(res);
        if (res.statusCode == 200) {
          console.log('成功');
          //添加列表
          if (res.data.list == null) {
            self.setData({
              ifnum: false
            })
          } else if (res.data.list.length < 6) {
            self.setData({
              ifnum: false
            });
          }
          self.setData({
            detail: self.data.detail.concat(res.data.list),

          });
          self.setData({
            Length: this.data.detail.length,
          });

          self.incise();
          // console.log('===haha===');
          // console.log(this.data.detail);
        } else {
          console.log("index.js wx.request CheckCallUser statusCode：" + res.statusCode);
        }
      },
      error: () => {
        console.log('失败！');
      }
    })
  },

  //加购物车
  addshop(e) {
    let that = this;
    let index = e.target.dataset.index;
    let item = that.data.detail[index];
    let vip = getApp().globalData.vipState;
    let price = 0;
    if (vip == 1) {
      price = item.goods.goodsPrice;
    } else {
      price = item.newPrice;
    }
    console.log(price);
    console.log(vip)
    console.log(that.data.userID);
    let userId = null;
    if (that.data.userID == null)
    {
      userId = wx.getStorageSync('userID');
    } else {
      userId = that.data.userID;
    }
    wx.request({
      url: 'https://localhost:80/cart/add',
      method: 'POST',
      data: {
        'cartGoods': {
          'goodsId': item.goods.goodsId,
          'goodsPrice': item.goods.goodsPrice
        },
        'cartNum': 1,
        'cartPrice': price,
        'cartUser': userId
      },

      success: res => {
        console.log('成功');
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 500
        })
      },
      error: res => {
        console.log('失败！');
        wx.showToast({
          title: '添加失败',
          icon: 'none',
          duration: 500
        })
      }

    })
  },

  //跳转函数
  jumpTo(e) {
    let self = this;
    console.log('准备跳转');
    console.log(e.currentTarget.dataset.index);
    let num = e.currentTarget.dataset.index;
    let id = self.data.detail[num].goods.goodsId;
    wx.navigateTo({
      url: '../../component/details/details?goodsId=' + id,

      success: function (res) {
        // success
        console.log('成功跳转');
      },
      fail: function () {
        // fail
        console.log('跳转失败');
      },
      complete: function () {
        // complete
        console.log('已请求');
      }
    })
  },

  //切割图片字符串函数
  incise() {
    let self = this;
    for (let index in self.data.detail) {
      //整合了arr数组存放了正确的图片地址
      let str = self.data.detail[index].goods.goodsImg.toString();
      let s = "detail[" + index + "].goods.goodsImg";
      let arr = str.split(',');
      self.setData({
        [s]: arr,
      })
    }
    console.log('--------------------------------');
    console.log(self.data.detail);
  },
})